package com.pattern.creational_pattern.proptype_pattern;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author Ping
 * @Date 2022/3/20 23:47
 **/
public class BookStore {
    // propotype manager
    static Map<String, Book> bookMap ;
    static {
//        bookMap = new Hashtable<>( ) ;
        bookMap = new ConcurrentHashMap<>() ;
        // adding all samples
        bookMap.put("book1", new Book("book1")) ;
        bookMap.put("book2", new Book("book2")) ;

    }
    // thread security
    public Book getCopy(String name) {
        return (Book) bookMap.get(name).clone() ;
    }
    public static void main(String args[]) {
        BookStore store = new BookStore() ;
        System.out.println(store.getCopy("book1")) ;
        System.out.println(store.getCopy("book1")) ;
        System.out.println(store.getCopy("book1")) ;
     }
}
